﻿using Abp.Domain.Repositories;
using NavGuide.Guide.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Abp.AutoMapper;

namespace NavGuide.Guide
{
    public class NavigationAppService : NavGuideAppServiceBase, INavigationAppService
    {
        private readonly IRepository<Navigation, string> _navigationRepo;
        public NavigationAppService(IRepository<Navigation, string> navigationRepo)
        {
            _navigationRepo = navigationRepo;
        }
        public NavigationDto Get(string id)
        {
            var entity = _navigationRepo.GetAll().FirstOrDefault(s => s.Id == id);
            if (entity != null)
                return entity.MapTo<NavigationDto>();
            return null;
        }
        public NavigationDto GetTree(string id)
        {
            Navigation entity = new Navigation() { Id = "0", DisplayName = "收藏夹" };
            List<Navigation> firstList;
            if (string.IsNullOrWhiteSpace(id))
            {
                firstList = _navigationRepo.GetAll().Where(s => s.ParentId == "1" && s.IsLeaf == false).ToList();
            }
            else
            {
                firstList = _navigationRepo.GetAll().Where(s => s.Id == id && s.IsLeaf == false).ToList();
            }
            foreach (var item in firstList)
            {
                var data = GetTreeNode(item);
                entity.Items.Add(data);
            }
            return entity.MapTo<NavigationDto>();
        }
        private Navigation GetTreeNode(Navigation entity)
        {
            Navigation node = new Navigation()
            {
                Id = entity.Id,
                DisplayName = entity.DisplayName,
                ParentId = entity.ParentId
            };
            var childs = _navigationRepo.GetAll().Where(s => s.ParentId == entity.Id && s.IsLeaf == false).ToList();
            foreach (var item in childs)
            {
                var data = GetTreeNode(item);
                node.Items.Add(data);
            }
            return node;
        }

        public List<NavigationDto> GetList(string search, string pid)
        {
            var query = _navigationRepo.GetAll();
            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(s => s.DisplayName.Contains(search) && s.IsLeaf == true);
            }
            else if (!string.IsNullOrWhiteSpace(pid))
            {
                query = query.Where(s => s.ParentId == pid && s.IsLeaf == true);
            }
            else
            {
                return new List<NavigationDto>();
            }
            var data = query.OrderByDescending(s => s.DisplayName).ToList();
            var dtos = data.MapTo<List<NavigationDto>>();
            return dtos;
        }
         
        public bool HasRoot()
        { 
            return _navigationRepo.GetAll().Any(s => s.ParentId == null);
        }

        public string Import(NavigationDto dto)
        {
            if (!string.IsNullOrEmpty(dto.DisplayName))
            {
                if (_navigationRepo.Count(s => s.DisplayName == dto.DisplayName) == 0)
                {
                    Navigation entity = new Navigation()
                    {
                        ParentId = dto.ParentId,
                        Name = dto.DisplayName,
                        DisplayName = dto.DisplayName,
                        Url = dto.Url,
                        IsLeaf = dto.IsLeaf,
                        Icon = "",
                        Account = "",
                        Key = ""
                    };

                    var newItem = _navigationRepo.Insert(entity);
                    return newItem.Id;
                }
            }
            return "";
        }

        public void Add(NavigationDto dto)
        {
            if (string.IsNullOrEmpty(dto.DisplayName))
            {
                throw new Exception("名称不能为空");
            }
            if (_navigationRepo.Count(s => s.DisplayName == dto.DisplayName) > 0)
            {
                throw new Exception("名称不能重复");
            }

            Navigation entity = new Navigation()
            {
                Name = dto.DisplayName,
                DisplayName = dto.DisplayName,
                Icon = dto.Icon,
                Url = dto.Url,
                Account = dto.Account,
                Key = dto.Key,
                ParentId = dto.ParentId
            };

            var newItem = _navigationRepo.Insert(entity);

            var parent = _navigationRepo.GetAll().FirstOrDefault(s => s.Id == dto.ParentId);
            if (parent != null)
            {
                parent.Items.Add(newItem);
            }
        }

        public void Edit(NavigationDto dto)
        {
            if (string.IsNullOrEmpty(dto.DisplayName))
            {
                throw new Exception("名称不能为空");
            }

            var entity = _navigationRepo.GetAll().FirstOrDefault(s => s.Id == dto.Id);

            entity.Name = dto.DisplayName;
            entity.DisplayName = dto.DisplayName;
            entity.Icon = dto.Icon;
            entity.Url = dto.Url;
            entity.Account = dto.Account;
            entity.Key = dto.Key;

            var newItem = _navigationRepo.Update(entity);

        }
    }
}
